﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;

namespace Charities
{
    public partial class WorthiesTypes : Form
    {
        int userId = -1;
        public bool is_loaded = false;
        
        public WorthiesTypes(int id,Form m)
        {
            this.MdiParent = m;
            userId = id;
            InitializeComponent();
        }
        ModelContainer ctx = new CharitiesLib.ModelContainer();

        private void WorthiesTypes_Load(object sender, EventArgs e)
        {

            User user = ctx.Users.Single(u => u.Id == userId);
            foreach (var Item in user.Role.Permissions)
            {
                switch (Item.Id)
                {
                    case 34:
                        btnAdd.Visible = true;
                        break;
                    case 36:
                        btnDelete.Visible = true;
                        break;
                    case 35:
                        btnModefy.Visible = true;
                        break;
                    default:
                        break;
                }
            }

            //List<WorthyType> wt = (from w in ctx.WorthyTypes select w).ToList();
            worthyTypeBindingSource.DataSource = ctx.WorthyTypes.ToList();
            WorthyTypeComboBox.DataSource = worthyTypeBindingSource;
            btnModefy.Enabled = false;
            btnDelete.Enabled = true;
            is_loaded = true;

            // add this form to window list
            ((Forms.FormMDI)this.MdiParent).addFormToWindowsList(this);

            
        }

        private void WorthyTypeComboBox_TextChanged(object sender, EventArgs e)
        {
            btnModefy.Enabled = true;
            btnAdd.Enabled = true;
            btnDelete.Enabled = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                 WorthyType tmp = ctx.WorthyTypes.Single(c => c.Name == WorthyTypeComboBox.Text);
                 MessageBox.Show("نوع المستحق موجود مسبقاً");
            }
            catch (Exception)
            {
                WorthyType w = new WorthyType();
                w.Name = WorthyTypeComboBox.Text;
                w.InsertDate = DateTime.Now;
                w.InsertUId = 1;
                ctx.WorthyTypes.Add(w);
                ctx.SaveChanges();
                worthyTypeBindingSource.DataSource = null;
                worthyTypeBindingSource.DataSource = ctx.WorthyTypes.ToList();
                WorthyTypeComboBox.DataSource = worthyTypeBindingSource;
            }
        }

        private void btnModefy_Click(object sender, EventArgs e)
        {
            try
            {
                WorthyType tmp = ctx.WorthyTypes.Single(c => c.Id == val);
                tmp.Name = WorthyTypeComboBox.Text;
                tmp.UpdateDate = DateTime.Now;
                tmp.UpdateUId = userId;
                ctx.SaveChanges();
                worthyTypeBindingSource.DataSource = null;
                worthyTypeBindingSource.DataSource = ctx.WorthyTypes.ToList();
                WorthyTypeComboBox.DataSource = worthyTypeBindingSource;
            }
            catch (Exception)
            {

                MessageBox.Show("نوع المستحق موجود مسبقاً");
            }
          
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int? id = (int?)WorthyTypeComboBox.SelectedValue;
                if (id != null)
                {
                    if (id > 0)
                    {
                        WorthyType WE = ctx.WorthyTypes.Single(c => c.Id == id);
                        if (WE.Worthies.Count() > 0)
                        {
                            MessageBox.Show("لايمكن حذف نوع مستحق مرتبط بأحد المستحقين!");
                        }
                        else
                        {
                            ctx.WorthyTypes.Remove(WE);
                            ctx.SaveChanges();
                            worthyTypeBindingSource.DataSource = null;
                            worthyTypeBindingSource.DataSource = ctx.WorthyTypes.ToList();
                            WorthyTypeComboBox.DataSource = worthyTypeBindingSource;

                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("يرجى التحقق من الاسم");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            is_loaded = false;
        }
        int val = 0;
        private void WorthyTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string valname = WorthyTypeComboBox.Text;
                WorthyType WE = ctx.WorthyTypes.Single(c => c.Name == valname);
                 val=WE.Id ;
                btnModefy.Enabled = true;
                btnAdd.Enabled = false;
                btnDelete.Enabled = true;
            }
            catch 
            {

                
            }
          
        }

        private void WorthiesTypes_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;

            // remove from the windows list
            ((Forms.FormMDI)this.MdiParent).removeFormFromWindowsList(this);

        }

        private void WorthiesTypes_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}
